package com.zhiyou100.xml.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class Demo1 {

    public static void main(String[] args) throws IOException {

        //获取一个类加载器
        ClassLoader loader = Demo1.class.getClassLoader();
        //加载xml文件，将其转换为一个URL资源
        URL resource = loader.getResource("test.xml");
        //获取资源路劲
        String path = resource.getPath();
        System.out.println("path = " + path);
        //利用jsoup加载配置文件
        Document document = Jsoup.parse(new File(path), "UTF-8");
        //根据标签名称获取元素
        Elements elements = document.getElementsByTag("user");
        System.out.println("user标签元素的个数：" + elements.size());
        //遍历Elements
        for (Element element : elements) {
            Elements ages = element.getElementsByTag("age");
            for (Element age : ages) {
                //获取标签的文本内容
                System.out.println(age.text());
            }
        }
    }

}
